package com.example.wang.yuejian;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.wang.yuejian.Model.UserModel;
import com.example.wang.yuejian.bean.LoginBean;

public class LoginActivity extends AppCompatActivity {
    private EditText userView,passwordView;
    private Button loginButton,signupButton;
    private RetrofitListener<LoginBean> listener;
    private UserModel model;
    private int userid;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        userView = (EditText) findViewById(R.id.edit_username);
        passwordView = (EditText) findViewById(R.id.edit_password);
        loginButton= (Button) findViewById(R.id.button_login);
        signupButton= (Button) findViewById(R.id.button_sign_up);
        listener=new RetrofitListener<LoginBean>() {
            @Override
            public void onResponse(LoginBean l) {
                if(l.getUserid()!=0)
                {
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    intent.putExtra("id",userid);
                    startActivity(intent);
                    finish();

                }
                else{
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(LoginActivity.this,msg,Toast.LENGTH_SHORT).show();

            }
        };
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               model=new UserModel();
                model.login(userView.getText().toString(),passwordView.getText().toString(),listener);
            }
        });
        signupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }
}
